﻿using Newtonsoft.Json.Linq;
using System;
using System.IO;
using System.Linq;
using System.Web;

namespace UeditorConfigs
{
    /// <summary>
    /// 版 本 V1.0.0.0 
    /// Copyright (c) 2013-2017 重庆海特科技发展有限公司
    /// 创建人：框架开发组
    /// 日 期：2017.03.07
    /// 描 述：百度编辑器UE配置文件操作文件操作
    /// </summary>
    public static class UeditorConfig
    {
        /// <summary>
        /// 文件保存根目录
        /// </summary>
        public static string UpLoadRoot { get { return HttpContext.Current.Server.MapPath("~/"); } }

        private static readonly bool noCache = true;
        private static JObject BuildItems()
        {
            var json = File.ReadAllText(HttpContext.Current.Server.MapPath("~/Scripts/UEditorjs/config/config.json"));
            return JObject.Parse(json);
        }

        public static JObject Items
        {
            get
            {
                if (noCache || _Items == null)
                {
                    _Items = BuildItems();
                }
                return _Items;
            }
        }
        private static JObject _Items;


        public static T GetValue<T>(string key)
        {
            return Items[key].Value<T>();
        }

        public static String[] GetStringList(string key)
        {
            return Items[key].Select(x => x.Value<String>()).ToArray();
        }

        public static String GetString(string key)
        {
            return GetValue<String>(key);
        }

        public static int GetInt(string key)
        {
            return GetValue<int>(key);
        }

        public static string GetUpload(string relativePath)
        {
            var path = relativePath.Replace(@"/", @"\").Replace(@"\\", @"\").TrimStart('\\');

            path = Path.Combine(UpLoadRoot, path);
            return path;
        }
    }
}
